
var callBackArray = {};
var id = 1000;
window.WebViewJavascriptBridge = {
    callHandler: function (functionString, params, callBack) {
        window.WebViewJavascriptBridge.call(functionString, params, callBack);
    },
    
    invoke: function (functionString, params, callBack) {
        window.WebViewJavascriptBridge.call(functionString, params, callBack);
    },

    call: function (functionString, params, callBack) {
        var timestamp = new Date().getTime();
        var methodName = functionString;
        var callBackName = callBack;
        if(!methodName) {
            return;
        }

        var jsonObject = {};
        if (typeof params == "Object") {
            jsonObject = params;
        } else {
            try {
                jsonObject = JSON.parse(params);
            } catch (e) {
                jsonObject = params;
            }
        }
        var message = new Object();
        message.methodName = methodName;
        if(params) message.params = params;
        if(callBackName) {
            message.callBackName = id++ + "";
            callBackArray[message.callBackName] = callBack
        }
        message.timestamp = timestamp;
        window.webkit.messageHandlers.mpDispatchMessage.postMessage(message);
    },

    callBackMessage: function (messageId,data) {
        callBack = callBackArray[messageId];
        if (callBack) {
            callBack(data);
            delete callBackArray.messageId;
        }
    }

}

